/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2023-11-01 22:05:37
 * @LastEditors: sueRimn
 * @LastEditTime: 2023-11-26 22:37:43
 */
import Vue from 'vue'
import Vuex from 'vuex'
// import VuexPersistence from 'vuex-persist'
import createPersistedstate from 'vuex-persistedstate'

import getters from './getters'

Vue.use(Vuex)

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)

// const vuexLocal = new VuexPersistence({
//   storage: {
//     getItem: key => uni.getStorageSync(key),
//     setItem: (key, value) => uni.setStorageSync(key, value),
//     removeItem: key => uni.removeStorageSync(key)
//   }
// })
// you do not need `import app from './modules/app'`
// it will auto require all vuex module from modules file
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  // set './app.js' => 'app'
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

const store = new Vuex.Store({
  modules,
  getters,
  plugins: [
    createPersistedstate({
      storage: {
        getItem: key => uni.getStorageSync(key),
        setItem: (key, value) => uni.setStorageSync(key, value),
        removeItem: key => uni.removeStorageSync(key)
      }
    })
  ]
})

export default store
